/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.utils;

import javax.microedition.lcdui.Font;
import java.util.Vector;

/**
 * Created by: recursv
 * Date: 3/08/2009
 * Time: 10:59:38 AM
 */
public class StringList {
    private final Vector _vector = new Vector();

    public StringList(String line, Font font, int width) {
        add(line, font, width);
    }

    public StringList(String str, char separator) {
        add(str, separator);
    }

    public StringList(String[] strings) {
        add(strings);
    }

    public StringList(String value) {
        add(value);
    }

    public StringList() {
    }

    private void add(String str, char separator) {
        StringBuffer temp = new StringBuffer();
        char c;
        for (int i = 0; i < str.length(); i++) {
            if ((c = str.charAt(i)) != separator) {
                temp.append(c);
            } else {
                _vector.addElement(temp.toString());
                temp.delete(0, temp.length());
            }
        }
        _vector.addElement(temp.toString());
    }

    private void add(String[] strings) {
        for (int i = 0; i < strings.length; i++)
            _vector.addElement(strings[i]);
    }

    public String elementAt(int i) {
        return (String) _vector.elementAt(i);
    }

    public int size() {
        return _vector.size();
    }

    public String[] toArray() {
        String[] strings = new String[_vector.size()];
        for (int i = 0; i < _vector.size(); i++)
            strings[i] = (String) _vector.elementAt(i);
        return strings;
    }

    public void add(String s) {
        _vector.addElement(s);
    }

    public void add(String line, Font font, int width) {
        StringBuffer buffer = new StringBuffer();
        for (int index = 0; index < line.length(); index++) {
            if (line.charAt(index) == '\n') {
                _vector.addElement(buffer.toString());
                buffer.delete(0, buffer.length());
            } else {
                if (font.stringWidth(buffer.toString()) >= width) {
                    int ref;
                    for (ref = buffer.length() - 1; ref > 0; ref--) {
                        if (buffer.charAt(ref) == ' ' || buffer.charAt(ref) == '\t')
                            break;
                    }
                    if (ref > 0) {
                        int lengthSoFar = buffer.length();
                        buffer.delete(ref, lengthSoFar);
                        index -= lengthSoFar - ref - 1;
                    } else {
                        buffer.delete(buffer.length() - 1, buffer.length());
                        index -= 1;
                    }

                    _vector.addElement(buffer.toString());
                    buffer.delete(0, buffer.length());
                }
                buffer.append(line.charAt(index));
            }
        }
        _vector.addElement(buffer.toString());
    }

    public void removeAll() {
        _vector.removeAllElements();
    }

    public void insertAt(String value, int index) {
        _vector.insertElementAt(value, index);
    }

    public boolean contains(String s) {
        for (int index = 0; index < _vector.size(); index++)
            if (_vector.elementAt(index).equals(s))
                return true;
        return false;
    }

    public String get(int index) {
        return (String) _vector.elementAt(index);
    }

    public void pruneEmptyStrings() {
        for (int index = _vector.size() - 1; index >= 0; index--) {
            if (((String) _vector.elementAt(index)).trim().length() == 0)
                _vector.removeElementAt(index);
        }
    }
}


